package com.lemon.smartsafety.utils;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.util.Log;

import com.baidu.location.BDLocation;
import com.lemon.smartsafety.R;
import com.lemon.smartsafety.utils.location.LocationBaiDuUtils;

/**
 * 项目名称：workspace
 * 类描述：
 * 创建人：Fargo
 * 创建时间：2016/8/12 14:19
 * 修改人：Fargo
 * 修改时间：2016/8/12 14:19
 * 修改备注：
 */
public class LocationUtil {

    private LocationBaiDuUtils locationBaiDuUtils;
    private Context context;
    private ProgressDialog progressDialog;


    private String mAddress = null;
    private String mLngLat = null;

    private String mLng = null;
    private String mLat = null;
    private String mCountry = null;
    private BDLocation mlocation = null;

    private CallBackInterface mBack = null;

    public interface CallBackInterface {
        public void callBackFunction();

    }

    public void setBack(CallBackInterface back) {
        this.mBack = back;
    }


    public LocationUtil(Context context) {
        this.context = context;
    }


    public String getmCountry() {
        return mCountry;
    }

    public String getmAddress() {
        return mAddress;
    }

    public String getmLngLat() {
        return mLngLat;
    }

    public String getmLng() {
        return mLng;
    }

    public String getmLat() {
        return mLat;
    }

    public BDLocation getMlocation() {
        return mlocation;
    }


    public void getLoacation(CallBackInterface callBackInterface) {
        mBack = callBackInterface;

        locationBaiDuUtils = new LocationBaiDuUtils(context);
        locationBaiDuUtils.setCallBackListen(new LocationBaiDuUtils.CallBackInterface() {
            @Override
            public void callBackFunction(boolean Success, BDLocation location) {
                LogUtils.LOGE("初始化本地时时地理位置成功：" + location.getAddrStr());
                mlocation = location;
                mAddress = location.getAddrStr();
                mCountry = location.getCountry();
                mLngLat = location.getLongitude() + "," + location.getLatitude();
                mLng = String.valueOf(location.getLongitude());
                mLat = String.valueOf(location.getLatitude());

                if (mBack != null) {
                    mBack.callBackFunction();
                }
            }
        });
        locationBaiDuUtils.initLocation();

    }


    public void showLocationDialog(CallBackInterface callBackInterface) {
        String str1 = context.getResources().getString(R.string.Making_sure_your_location);
        progressDialog = new ProgressDialog(context);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setMessage(str1);

        progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {

            public void onCancel(DialogInterface arg0) {
                if (progressDialog.isShowing()) {
                    progressDialog.dismiss();
                }
                Log.d("map", "cancel retrieve location:取消定位");
                if (mBack != null) {
                    mBack.callBackFunction();
                }
            }
        });

        progressDialog.show();
    }


    public void dissmissLocationDialog() {
        if (progressDialog.isShowing()) {
            progressDialog.dismiss();
        }
    }


    public void onDestroy() {
        if (mlocation == null&&locationBaiDuUtils!=null) {
            locationBaiDuUtils.onDestroy();
        }

    }

}